//
// Created by weiyixing on 24-11-13.
//

#include "TextHelper.h"
#include <iostream>
#include <sstream>
#include <fstream>
void TextHelper::run() {
  readText();
  getWordCont();
  displayResult();
}
void TextHelper::readText() {
     std::ifstream txtFile(_filePath);

     if(!txtFile){
       throw std::runtime_error("counld not open file: "+filePath);
     }

     std::stringstream stringTextStream;
     stringTextStream<<txtFile.rdbuf();
     std::string word;
     while (stringTextStream >> word ){
       _text.push_back(word);
     }
}

bool TextHelper::isExist(const std::string& word) {
     bool flag=(_prohibitedWords.find(word)!=_prohibitedWords.end());
     return flag;
}


void TextHelper::getWordCont() {
     for(std::string word :_text){
      if(isExist(word)){
        auto it=_wordCounter.find(word);
        if(it!=_wordCounter.end())
          ++it->second;
        else
          _wordCounter.insert({word,0});
      }
     }
}

void TextHelper::displayResult() {

     for(auto & it : _wordCounter){
      std::cout<<"word: "<<it.first<<"count: "<<it.second<<std::endl;

     }
}
